package com.corner.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.corner.R;
import com.corner.presenter.imp.SearchLocationPresenter;
import com.corner.ui.adapter.SelectLocationAdapter;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.fragment.LocationListFragment;
import com.corner.ui.listener.TextChangeListener;
import com.corner.ui.view.ISearchLocationView;
import com.corner.utils.AppManager;
import com.corner.utils.SoftInputUtil;
import com.corner.utils.view.FindView;
import com.corner.utils.view.widget.XListView;

import java.util.List;

/**
 * Created by Administrator on 2015/7/24，10.25.
 * 描述：
 */
public class SearchLocationActivity extends BaseActivity<SearchLocationPresenter> implements ISearchLocationView, TextView.OnEditorActionListener, View.OnClickListener, AdapterView.OnItemClickListener {
    public static final String INITENT_ID = "INITENT_ID";
    public static final int SEARCH_LOCATION = 1;
    public static final String SEARCH_LOCATION_S = "searchLocation";
    public static final String SEARCH_LOCATION_S_NAME = "searchLocationName";
    @FindView(R.id.lv_location_list)
    private XListView lvLocations;
    @FindView(R.id.tv_search_location_cancel)
    private TextView tvCancel;
    @FindView(R.id.tv_search_text_delete)
    private ImageView tvDelete;
    @FindView(R.id.et_serch_locaton_search)
    private EditText etSearch;

    private SelectLocationAdapter adapter;
    private List<PoiItem> poiitems;
    private View header;
    private TextView tvName;
    private TextView tvAddr;
    private int intentId;

    @Override
    protected SearchLocationPresenter createP() {
        return new SearchLocationPresenter();
    }

    @Override
    protected void setOnClick() {
        etSearch.addTextChangedListener(new TextChangeListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(etSearch.getText()))
                    tvAddr.setText(getString(R.string.search_location_please_search));
                else
                    tvAddr.setText(getString(R.string.select_location_use_this_text));
                header.setVisibility(View.VISIBLE);
                tvName.setText(etSearch.getText());
                presenter.requestLocation(etSearch.getText().toString(), SearchLocationActivity.this);
            }
        });
        tvCancel.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
        etSearch.setOnEditorActionListener(this);
        lvLocations.setPullRefreshEnable(false);
        lvLocations.setAutoLoadEnable(true);
        lvLocations.addHeaderView(header);
        lvLocations.setPullLoadEnable(false);
        lvLocations.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                presenter.loadMoreLocation(SearchLocationActivity.this);
            }
        });
        lvLocations.setOnItemClickListener(this);
        header.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        presenter.requestLocation("", this);
        intentId = getIntent().getIntExtra(INITENT_ID, 0);
    }

    @Override
    public void initView() {
        super.initView();
        initHeader();
    }

    private void initHeader() {
        header = View.inflate(this, R.layout.lv_select_location_item, null);
        tvName = (TextView) header.findViewById(R.id.tv_select_location_item_name);
        tvAddr = (TextView) header.findViewById(R.id.tv_select_location_item_address);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_search_locations);
    }

    @Override
    public void showPois(List<PoiItem> poiItems) {
        if (adapter == null) {
            adapter = new SelectLocationAdapter(poiItems, this);
            lvLocations.setAdapter(adapter);
        } else {
            adapter.changeList(poiItems);
        }
        this.poiitems = poiItems;
    }

    @Override
    public void setLoadMoreEnable(boolean hasMore) {
        lvLocations.setPullLoadEnable(hasMore);
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        presenter.requestLocation(etSearch.getText().toString(), SearchLocationActivity.this);
        return true;
    }

    @Override
    public void onClick(View v) {
        if (v == header) {
            if (TextUtils.isEmpty(etSearch.getText())) {
                return;
            }
            switch (intentId) {
                case CreateInfoActivity.SELECT_LOCATION:
                    Intent intent = getIntent();
                    intent.putExtra(CreateInfoActivity.SELECT_LOCATION_RESULT_TEXT, etSearch.getText().toString().trim());
                    setResult(CreateInfoActivity.SELECT_LOCATION, intent);
                    AppManager.getInstance().finishActivity(this);
                    overridePendingTransition(0, 0);
                    break;
                case LocationListFragment.SEARCH_LOCATION:
                    intent = getIntent();
                    intent.putExtra(ChannelActivity.INTENT_ID, SEARCH_LOCATION);
                    intent.putExtra(SEARCH_LOCATION_S_NAME, etSearch.getText().toString().trim());
                    intent.setClass(this, ChannelActivity.class);
                    intent.removeExtra(SEARCH_LOCATION_S);
                    startActivity(intent);
                    break;
            }
        } else {
            switch (v.getId()) {
                case R.id.tv_search_text_delete:
                    etSearch.setText(null);
                    break;
                case R.id.tv_search_location_cancel:
                    SoftInputUtil.hideSoftInput(this, etSearch);
                    AppManager.getInstance().finishActivity(this);
                    break;
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = getIntent();
        switch (intentId) {
            case CreateInfoActivity.SELECT_LOCATION:
                PoiItem item = poiitems.get((int) id);
                LatLonPoint point = item.getLatLonPoint();
                intent.putExtra(CreateInfoActivity.SELECT_LOCATION_RESULT_POI, new com.corner.ui.parcel.PoiItem(point.getLatitude(), point.getLongitude(), item.getSnippet(), item.getTitle(), item.getProvinceName(), item.getCityName()));
                setResult(CreateInfoActivity.SELECT_LOCATION, intent);
                AppManager.getInstance().finishActivity(this);
                overridePendingTransition(0, 0);

                break;
            case LocationListFragment.SEARCH_LOCATION:
                item = poiitems.get((int) id);
                point = item.getLatLonPoint();
                intent.putExtra(ChannelActivity.INTENT_ID, SEARCH_LOCATION);
                intent.putExtra(SEARCH_LOCATION_S, new com.corner.ui.parcel.PoiItem(point.getLatitude(), point.getLongitude(), item.getSnippet(), item.getTitle(), item.getProvinceName(), item.getCityName()));
                intent.setClass(this, ChannelActivity.class);
                intent.removeExtra(SEARCH_LOCATION_S_NAME);
                startActivity(intent);
                break;
        }
    }
}
